package tako.wisers.com.android.apis.view;

import java.lang.reflect.Field;
import java.util.Hashtable;
import java.util.Map;

import tako.wisers.com.android.apis.R;

public class ViewNameIDConverse {
	private static Map<String, Integer> viewNameMap  = new Hashtable<String, Integer>();
	private static Map<Integer, String> viewIdMap  = new Hashtable<Integer, String>();
	
	static {
		init();
	}
	
	private static void init(){
		try {
			setMap(R.drawable.class.newInstance());
			setMap(R.id.class.newInstance());
			setMap(R.layout.class.newInstance());
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
	}
	
	private static void setMap(Object obj) throws IllegalArgumentException, IllegalAccessException{
		Field[] fields = obj.getClass().getFields();
		for(Field field : fields){
			String name = field.getName();
			int id = field.getInt(obj);
			viewNameMap.put(name, id);
			viewIdMap.put(id, name);
		}
	}
	
	
	public static int getIdFromName(String name){
		if(name == null) return 0;
		Integer id =  viewNameMap.get(name);
		return id != null ? id : 0 ;
	}
	
	public static String getNameFromId(int id){
		return viewIdMap.get(id);
	}
}
